package com.jinxin.platform.cddjnhjcxtsysset.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CddjnhjcxtsyssetHomeRange;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CddjnhjcxtsyssetHomeReleva;
import com.jinxin.platform.cddjnhjcxtsysset.common.Constant;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysUserVO;
import com.jinxin.platform.cddjnhjcxtsysset.mapper.CddjnhjcxtsyssetHomeRangeMapper;
import com.jinxin.platform.cddjnhjcxtsysset.rpc.CddjnhjcxtsyssetRpc;
import com.jinxin.platform.cddjnhjcxtsysset.service.ICddjnhjcxtsyssetHomeRangeService;
import com.jinxin.platform.cddjnhjcxtsysset.utils.IdWorker;
import com.jinxin.platform.cddjnhjcxtsysset.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author 那个男人
 * @Date 2022/5/11 13:48
 * @Version 1.0
 */
@Service
public class CddjnhjcxtsyssetHomeRangeServiceImpl implements ICddjnhjcxtsyssetHomeRangeService {
    @Autowired
    private CddjnhjcxtsyssetHomeRangeMapper homeRangeMapper;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CddjnhjcxtsyssetRpc cddjnhjcxtsyssetRpc;

    @Override
    public CddjnhjcxtsyssetHomeRange selectOne(HashMap param) {
        return homeRangeMapper.selectOne(param);
    }

    @Override
    public JsonResult saveOrUpdate(CddjnhjcxtsyssetHomeRange record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        SysUserVO currentUser = cddjnhjcxtsyssetRpc.getCurrentUser();
        if(currentUser == null){
            return JsonResult.error("获取当前用户信息失败");
        }
        int count;
        if(StringUtil.isEmpty(record.getId())){
            record.setId(String.valueOf(idWorker.nextId()));
            String orgId = currentUser.getOrgId();
            if(Constant.ACCOUNT_ADMIN.equals(currentUser.getUserAccount())){
                orgId = Constant.STRING_ZERO;
            }
            record.setOrgId(orgId);
            count = homeRangeMapper.insert(record);
        }else {
            count = homeRangeMapper.updateByKey(record);
        }
        if(count > 0){
            return JsonResult.ok("操作成功",record.getId());
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }
}
